package com.hansen.library.ui.widget.textview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntDef;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.Gravity;

import com.hansen.library.R;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Drawable 和文字一起居中textview
 * @author han
 * @date 2019/9/18
 */
public class DrawableCenterTextView extends AppCompatTextView {

    private Drawable[] drawables;

    private int drawableLeftWidth, drawableTopWidth, drawableRightWidth, drawableBottomWidth;
    private int drawableLeftHeight, drawableTopHeight, drawableRightHeight, drawableBottomHeight;

    public static final int LEFT = 0;
    public static final int TOP = 1;
    public static final int RIGHT = 2;
    public static final int BOTTOM = 3;

    @IntDef({
            LEFT,
            TOP,
            RIGHT,
            BOTTOM
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface DrawGravity {
    }

    public DrawableCenterTextView(Context context) {
        this(context, null);
    }

    public DrawableCenterTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawableCenterTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setGravity(Gravity.CENTER);

        drawables = new Drawable[4];

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.DrawableCenterTextView);

        drawables[0] = array.getDrawable(R.styleable.DrawableCenterTextView_leftDrawable);
        drawables[1] = array.getDrawable(R.styleable.DrawableCenterTextView_topDrawable);
        drawables[2] = array.getDrawable(R.styleable.DrawableCenterTextView_rightDrawable);
        drawables[3] = array.getDrawable(R.styleable.DrawableCenterTextView_bottomDrawable);

        drawableLeftWidth = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_leftDrawableWidth, 0);
        drawableTopWidth = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_topDrawableWidth, 0);
        drawableRightWidth = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_rightDrawableWidth, 0);
        drawableBottomWidth = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_bottomDrawableWidth, 0);

        drawableLeftHeight = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_leftDrawableHeight, 0);
        drawableTopHeight = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_topDrawableHeight, 0);
        drawableRightHeight = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_rightDrawableHeight, 0);
        drawableBottomHeight = array.getDimensionPixelSize(R.styleable.DrawableCenterTextView_bottomDrawableHeight, 0);

        array.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Drawable drawableLeft = drawables[0];
        Drawable drawableTop = drawables[1];
        Drawable drawableRight = drawables[2];
        Drawable drawableBottom = drawables[3];

        if (drawableLeft != null) {
            setDrawable(drawableLeft, 0, drawableLeftWidth, drawableLeftHeight, canvas);
        }
        if (drawableTop != null) {
            setDrawable(drawableTop, 1, drawableTopWidth, drawableTopHeight, canvas);
        }
        if (drawableRight != null) {
            setDrawable(drawableRight, 2, drawableRightWidth, drawableRightHeight, canvas);
        }
        if (drawableBottom != null) {
            setDrawable(drawableBottom, 3, drawableBottomWidth, drawableBottomHeight, canvas);
        }
    }

    public void setCenterDrawable(@DrawableRes int left, @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) {
        Context context = getContext();

        setCenterDrawable(left != 0 ? ContextCompat.getDrawable(context, left) : null,
                top != 0 ?  ContextCompat.getDrawable(context, top) : null,
                right != 0 ?  ContextCompat.getDrawable(context, right) : null,
                bottom != 0 ?  ContextCompat.getDrawable(context, bottom) : null);
    }

    public void setCenterDrawable(Drawable drawableLeft, Drawable drawableTop, Drawable drawableRight, Drawable drawableBottom) {
        if(drawables == null || drawables.length != 4) {
            return;
        }

        drawables[0] = drawableLeft;
        drawables[1] = drawableTop;
        drawables[2] = drawableRight;
        drawables[3] = drawableBottom;

        postInvalidate();
    }



    private void setDrawable(Drawable drawable, int tag, int drawableWidth, int drawableHeight, Canvas canvas) {
        //获取图片实际长宽
        int width = drawableWidth == 0 ? drawable.getIntrinsicWidth() : drawableWidth;
        int height = drawableHeight == 0 ? drawable.getIntrinsicHeight() : drawableHeight;
        int left = 0, top = 0, right = 0, bottom = 0;

        int drawablePadding = getCompoundDrawablePadding();

        float centerX = (getWidth() + getPaddingLeft() - getPaddingRight()) / 2;
        float centerY = (getHeight() + getPaddingTop() - getPaddingBottom()) / 2;

        float halfTextWidth = getPaint().measureText(getText().toString().isEmpty() ? getHint().toString() : getText().toString()) / 2;
        Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
        float halfTextHeight = (fontMetrics.descent - fontMetrics.ascent) / 2;

        switch (tag) {
            case 0:
                left = (int) (centerX - halfTextWidth - drawablePadding - width);
                top = (int) (centerY - height / 2);
                right = left + width;
                bottom = top + height;
                break;
            case 2:
                left = (int) (centerX + halfTextWidth + drawablePadding);
                top = (int) (centerY - height / 2);
                right = left + width;
                bottom = top + height;
                break;
            case 1:
                left = (int) (centerX - width / 2);
                top = (int) (centerY - halfTextHeight - drawablePadding - height);
                right = left + width;
                bottom = top + height;
                break;
            case 3:
                left = (int) (centerX - width / 2);
                top = (int) (centerY + halfTextHeight + drawablePadding);
                right = left + width;
                bottom = top + height;
                break;
        }

        drawable.setBounds(left, top, right, bottom);

        canvas.save();
        drawable.draw(canvas);
        canvas.restore();
    }
}
